package com.drore.logapp.ui;

import android.view.View;
import android.widget.TextView;

import com.drore.app.LogRecordUtil;
import com.drore.logapp.R;
import com.drore.logapp.base.BaseActivity;
import com.drore.logapp.bean.BaseBean;
import com.drore.logapp.bean.LogBean;
import com.drore.logapp.mvp.AppContract;
import com.drore.logapp.mvp.AppPresenter;
import com.drore.www.logbeansecret.bean.EventBean;
import com.drore.www.logbeansecret.bean.NetConnectBean;
import com.drore.www.logbeansecret.bean.PhoneBean;

import java.util.List;


public class NetWorkActivity extends BaseActivity<AppPresenter, BaseBean> implements AppContract.AppView {
    private TextView content;
    private int page = 1;
    private StringBuffer stringBuffer = new StringBuffer();

    @Override
    public int getLayoutId() {
        return R.layout.activity_main2;
    }

    @Override
    protected void initView() {
        LogRecordUtil.setStartStayTime("NetWorkActivity");
        if (mPresenter == null) {
            mPresenter = getPresenter();
        }
        mPresenter.attachView(this, pContext);
        mPresenter.getLoganList(page);
        content = findViewById(R.id.content);
        findViewById(R.id.post_body).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.getLoganList(1);
            }
        });

        findViewById(R.id.get_body_error).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.loginToken();
            }
        });
        findViewById(R.id.post_body_error).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.getUserDetail();
            }
        });
        findViewById(R.id.post_body_query).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.getThingDetail();
            }
        });
        findViewById(R.id.time_out).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.getTimeOut();
            }
        });

        findViewById(R.id.clearlog).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stringBuffer = new StringBuffer();
                content.setText("内容\n" + stringBuffer.toString());
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LogRecordUtil.setEndStayTime("NetWorkActivity");
    }

    @Override
    public void showLogList(BaseBean<LogBean> logBeanList) {
        commit();
    }

    @Override
    public void showLogError(BaseBean<String> baseBean) {
//        content.setText("内容\n" + stringBuffer.toString());
        commit();
    }

    @Override
    public void showDataSuccess(BaseBean datas) {
        super.showDataSuccess(datas);
        commit();
    }

    @Override
    protected void onResume() {
        super.onResume();
        commit();
    }

    private void commit() {
        List<EventBean> listEvent = LogRecordUtil.getEventBeanList();
        List<PhoneBean> phoneBeanList = LogRecordUtil.getPhoneBeanList();
        List<NetConnectBean> netConnectBeanList = LogRecordUtil.getNetSatauBeanList();
        stringBuffer = new StringBuffer();
        stringBuffer.append("手机信息数量：" + phoneBeanList.size()
                + "\n事件信息数量：" + listEvent.size()
                + "\n请求信息数量：" + netConnectBeanList.size());
        for (NetConnectBean NetConnectBean : netConnectBeanList) {
            stringBuffer.append("\n" + NetConnectBean.getRequestUrl());
            stringBuffer.append("\n" + NetConnectBean.getRequestParams());
        }
        content.setText(stringBuffer.toString());
    }
}
